const topic = {
    state: {
        course: '',//刷题科目
        length: '',//题目数量
        all: "",//所有题目
        subjectIndex: 0,//当前题目位置
        subject: "",//当前题目
        subjectId: "",//当前题目
        //答题卡
        question: {},
        // 被选中
        radio: 0,
        yes: 0,
        no: 14,
    },
    mutations: {
        number(state) {
            state.yes = 0;
            state.no = state.length;
            for (var i in state.question) {
                if (state.question[i].user_answer[0] !== undefined) {
                    state.yes++;
                    state.no--;
                }
            }

            console.log(state.yes, state.no)
        },
        //科目
        GETCourse(state, data) {
            state.course = data;
        },
        //题数据
        GETTOPIC(state, data) {
            state.length = data.length
            state.all = data//全部题目
            state.subject = data[0]//当前题目
            state.subjectId = data[0].id//当前题目id

            let obj = {}
            data.map(el => {
                obj[el.id] = {
                    "user_answer": [],
                    report_type: el.ques_type
                }
            })
            state.question = obj//生成答题卡
        },
        //上一题
        subjectNext(state) {
            state.subjectIndex++
            let index = state.question[state.all[state.subjectIndex].id].user_answer

            state.subject = state.all[state.subjectIndex]//当前题目
            state.subjectId = state.all[state.subjectIndex].id//当前题目id
            state.radio = index[0] === undefined ? 0 : index[0]
        },
        subjectstaBack(state) {
            state.subjectIndex--;
            let index = state.question[state.all[state.subjectIndex].id].user_answer

            state.subject = state.all[state.subjectIndex]//当前题目
            state.subjectId = state.all[state.subjectIndex].id//当前题目id
            state.radio = index[0] === undefined ? 0 : index[0]
        },
        //跳转试题
        Jump(state, li) {
            state.subjectIndex = li;
            let index = state.question[state.all[state.subjectIndex].id].user_answer

            state.subject = state.all[state.subjectIndex]//当前题目
            state.subjectId = state.all[state.subjectIndex].id//当前题目id
            state.radio = index[0] === undefined ? 0 : index[0]
        },
        //选择事件
        optck(state, index) {
            state.question[state.all[state.subjectIndex].id].user_answer = [index]
            state.radio = index;

            this.commit('number')
        },
        kong(state) {
            state.question = {};
            state.radio = 0;
        }
    },
    actions: {
        GETTOPIC(context, data) {
            console.log(data)
            this.$http.examPoint(data).then(res => {
                console.log(res)
                context.commit('GETTOPIC', res.data.data)
            })
        },
        //获取所刷题的科目
        GETCourse(context, id) {
            this.$http.point(id).then(res => {
                context.commit('GETCourse', res.data.data[0])
            })
        }
    },
    module: {}
};

export default topic;